import type { Evaluate, EvaluateInfo, EvaluateRequestParams, Goods } from "@/types/Goods";
import type { Pagination, XtxResponse } from "@/types/Response";
import XtxRequestManager from "@/utils/XTXRequestManager";


export class GoodsAPI {
    //根据商品id获取商品的详细信息
    static getGoodsInfo(id: string) {
        return XtxRequestManager.instance.request<
            XtxResponse<Goods>,
            { id: string }
        >
            ({ url: "/goods", params: { id } })
    }
    //用于获取商品详情
    // id: 商品id, 传递了商品id表示同类推荐, 没传则表示猜你喜欢
    // limit: 限制请求的商品数量
    static getRelevantGoods(args?: { id?: string; limit?: number }) {
        //入过开发者传递了args对象，但是在args对象中没有传递limit
        if (typeof args !== "undefined" && typeof args.limit === "undefined") {
            //为limit参数设置默认值
            args.limit = 16
        } else {
            //开发者没有传递args对象
            //为limit参数设置默认值
            args = { limit: 16 }
        }
        return XtxRequestManager.instance.request<XtxResponse<Goods[]>>({
            url: "/goods/relevant",
            params: args,
        })
    }

    //获取热榜
    static getHotSaleGoods(id: string, type: 1 | 2 | 3, limit: number) {
        return XtxRequestManager.instance.request<XtxResponse<Goods[]>, {
            id: string; type: 1 | 2 | 3; limit: number
        }>({
            url: "/goods/hot",
            params: { id, type, limit }

        })
    }

    //获取商品评价信息
    static getEvaluateInfo(id: string) {
        return XtxRequestManager.instance.request<XtxResponse<EvaluateInfo>>({
            url: `https://mock.boxuegu.com/mock/1175/goods/${id}/evaluate`
        })
    }

    //获取商品评价列表
    static getEvaluateList(id: string, reqParams: EvaluateRequestParams) {
        return XtxRequestManager.instance.request<XtxResponse<Pagination<Evaluate>>, EvaluateRequestParams>({
            url: `https://mock.boxuegu.com/mock/1175/goods/${id}/evaluate/page`,
            data: reqParams
        })
    }

}